
package com.rt.appauthorize;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import javax.servlet.ServletException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
@Deprecated
public class AppAuthorizeServlet extends HttpServlet
{
    private static String RESOURCES_PATH = "http/html";
    
    protected String getFilePath(String fileName) {
        return AppAuthorizeServlet.class.getResource("").getPath() + "http/html" + fileName;
    }
    
    protected void returnResourceFile(String filePath, String uri, HttpServletResponse response) throws ServletException, IOException {
        if (filePath.endsWith(".html")) {
            response.setContentType("text/html; charset=utf-8");
        }
        if (filePath.endsWith(".jpg")) {
            byte[] bytes = AppAuthFileUtil.readByteArrayFromResource(filePath);
            if (bytes != null) {
                response.getOutputStream().write(bytes);
            }
            return;
        }
        InputStream is = Files.newInputStream(Paths.get(filePath));
        String text = AppAuthFileUtil.read(is);
        if (filePath.endsWith(".css")) {
            response.setContentType("text/css;charset=utf-8");
        }
        else if (filePath.endsWith(".js")) {
            response.setContentType("text/javascript;charset=utf-8");
        }
        response.getWriter().write(text);
    }
    
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();
        String requestURI = request.getRequestURI();
        response.setCharacterEncoding("utf-8");
        if (contextPath == null) {
            contextPath = "";
        }
        String uri = contextPath + servletPath;
        String path = requestURI.substring(contextPath.length());
        String htmlFilePath = this.getFilePath("/rtAuth/index");
        if ("/rtAuth/index".equals(path)) {
            this.returnResourceFile(htmlFilePath, uri, response);
        }
        if (!"/rtAuth/getMachineCode".equals(path)) {
            response.getWriter().print("error");
        }
    }
}
